package de.rotkiv.playlistgenerator.profile;

import java.util.HashMap;

import org.apache.log4j.Logger;

import de.rotkiv.common.properties.profile.IF_ProfileTags;
import de.rotkiv.common.util.Toolbox;

/**
 * @author Rotkiv
 *
 */
public class ProfileConverter implements IF_ProfileTags {

	private static final Logger logger = Logger.getLogger(ProfileConverter.class );


	public static IF_ProfilePlaylist convertFromPropertiesToProfileTO(HashMap<String, String> map) {

		String pathInput  			= map.get(PROFILE_PATH_INPUT);
		String pathOutput 			= map.get(PROFILE_PATH_OUTPUT);
		String playlistExtension  	= map.get(PROFILE_PLAYLIST_EXTENSION);
		String musicFileExtensions  = map.get(PROFILE_MUSIC_FILE_EXTENSIONS);
		String maxTracksPerPlaylist = map.get(PROFILE_MAX_TRACKS_PER_PLAYLIST);
		String pfadersetzung   		= map.get(PROFILE_PFADERSETZUNG);
		String pfadersetzungDurch   = map.get(PROFILE_PFADERSETZUNG_DURCH);
		String lastPathInput        = map.get(PROFILE_LAST_PATH_INPUT);
		String lastPathOutput       = map.get(PROFILE_LAST_PATH_OUTPUT);
		String relativerPfadStr     = map.get(PROFILE_RELATIVER_PFAD);
		//
		ProfilePlaylistImpl to = new ProfilePlaylistImpl();

		to.setPathInput(pathInput);
		to.setPathOutput(pathOutput);
		to.setPlaylistExtension(playlistExtension);
		to.setPathOutputPfadersetzung(pfadersetzung);
		to.setPathOutputPfadersetzungDurch(pfadersetzungDurch);
		to.setLastPathInput(lastPathInput);
		to.setLastPathOutput(lastPathOutput);

		int maxTracksInt = getIntValue(maxTracksPerPlaylist);
		to.setMaxTracksPerPlaylist(maxTracksInt);

		boolean isSelectedRelativerPfad = Boolean.parseBoolean(relativerPfadStr);
		to.setSelectedRelativerPfad(isSelectedRelativerPfad);

		String[] musicFileExtensionsArr = splitMusicFileExtensions(musicFileExtensions);
		to.setMusicFileExtensions(musicFileExtensionsArr);

		return to;
	}


	public static String[] splitMusicFileExtensions(String str) {
		String[] result = new String[0];
		if (str != null) {
			result = str.split(DELIMITER);
		}
		return result;
	}

	public static int getIntValue(String string) {
		int result = 0;

		if (string != null) {
			try {
				result = Integer.valueOf(string);
			} catch (NumberFormatException e) {
				logger.error("Fehler beim Einlesen der Max-Tracks : >" + string + "<");
			}
		}
		return result;
	}

	public static String getStringFromArray(String[] arr) {
		int counter     = 0;
		StringBuffer sb = new StringBuffer();
		if (arr != null) {
			counter = arr.length;
		}
		for (int i = 0; i < counter; i++) {
			String temp = arr[i];
			sb.append(temp);
			if (i < counter -1) {
				sb.append(DELIMITER);
			}
		}
		String result = sb.toString();
		return result;
	}

	public static HashMap<String, String> convertFromProfileToHashMap(IF_ProfilePlaylist profile) {
		HashMap<String, String> map = new HashMap<String, String>();

		String lastPathInput 			= profile.getLastPathInput();
		String lastPathOutput 			= profile.getLastPathOutput();
		int maxTracksPerPlaylist 		= profile.getMaxTracksPerPlaylist() ;
		String[] musicFileExtensionsArr = profile.getMusicFileExtensions() ;
		String pathInput 				= profile.getPathInput() ;
		String pathOutput 				= profile.getPathOutput() ;
		String pathOutputPfadersetzung 	= profile.getPathOutputPfadersetzung() ;
		String pfadersetzungDurch 		= profile.getPathOutputPfadersetzungDurch() ;
		String playlistExtension 		= profile.getPlaylistExtension() ;
		boolean relativerPfad   		= profile.isSelectedRelativerPfad() ;

		String musicFileExtensions 		= Toolbox.getStringFromArray(musicFileExtensionsArr, DELIMITER);

		map.put(PROFILE_PATH_INPUT, pathInput);
		map.put(PROFILE_PATH_OUTPUT, pathOutput);
		map.put(PROFILE_PLAYLIST_EXTENSION, playlistExtension);
		map.put(PROFILE_MUSIC_FILE_EXTENSIONS, musicFileExtensions);
		map.put(PROFILE_MAX_TRACKS_PER_PLAYLIST, String.valueOf(maxTracksPerPlaylist));
		map.put(PROFILE_PFADERSETZUNG, pathOutputPfadersetzung);
		map.put(PROFILE_PFADERSETZUNG_DURCH, pfadersetzungDurch);
		map.put(PROFILE_LAST_PATH_INPUT, lastPathInput);
		map.put(PROFILE_LAST_PATH_OUTPUT, lastPathOutput);
		map.put(PROFILE_RELATIVER_PFAD, String.valueOf(relativerPfad));
		//
		return map;
	}
}

